package com.avic.rong.search.adapter.homePageAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams;
import com.avic.rong.R;
import com.avic.rong.bean.product.GoodBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.minato.minastore.utils.AppUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Creater: Created by wangyz on 2019/9/18.
 * Email: 936804097@qq.com
 */
public class VBookAdapter extends DelegateAdapter.Adapter<VBookAdapter.VBookViewHolder> {


    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private LayoutParams mLayoutParams;
    private List<GoodBean> beanList = new ArrayList<>();

    public void setBeanList(List<GoodBean> beanList) {
        this.beanList = beanList;
    }

    public VBookAdapter(Context context, LayoutHelper layoutHelper, List<GoodBean> beanList) {
        this(context, layoutHelper, beanList, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public VBookAdapter(Context context, LayoutHelper layoutHelper, List<GoodBean> beanList, @NonNull LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.beanList = beanList;
        this.mLayoutParams = layoutParams;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public VBookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

        return new VBookViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_view_book, parent, false));
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull VBookViewHolder vBookViewHolder, int positon) {
        GoodBean bean = beanList.get(positon);

        String standardNo = "";
        if (!StringUtils.isEmpty(bean.getNo())) {
            standardNo = bean.getNo();
        }
        vBookViewHolder.tv_standard_name.setText(bean.getTitle().replaceFirst(standardNo, ConstantsUtils.EMPTY_STR).trim());

        if (StringUtils.isEmpty(bean.getAttrShow())) {
            vBookViewHolder.tv_standard_status.setVisibility(View.GONE);
        } else {
            vBookViewHolder.tv_standard_status.setVisibility(View.VISIBLE);
            vBookViewHolder.tv_standard_status.setText(bean.getAttrShow());
        }

        vBookViewHolder.tv_standard_no.setText(bean.getNo());
        vBookViewHolder.tv_standard_category.setText(bean.getTitle());

        vBookViewHolder.tv_book_desc_sub.setVisibility(View.VISIBLE);
        if (bean.getTypeId() == 2) {//图书
            String author = "";
            String publisDept = "";
            List<String> attrsList = bean.getAttrs();
            for (String attr : attrsList) {
                if (!StringUtils.isEmpty(attr)) {
                    if (attr.contains("91|") && attr.length() > 3) {
                        author = attr.substring(3);
                    } else if (attr.contains("95|") && attr.length() > 3) {
                        publisDept = attr.substring(3);
                    }
                }
            }
            vBookViewHolder.tv_book_desc.setMaxLines(1);
            vBookViewHolder.tv_book_desc.setText("作者:  " + author);
            vBookViewHolder.tv_book_desc_sub.setMaxLines(1);
            vBookViewHolder.tv_book_desc_sub.setText("出版社:  " + publisDept);
        }

        String imgUrl = bean.getImg();
        if (StringUtils.isEmpty(imgUrl)) {
            if (bean.getTypeId() == 2) {//图书
                vBookViewHolder.ll_no_picture.setVisibility(View.GONE);
                vBookViewHolder.tv_standard_status.setVisibility(View.GONE);
                vBookViewHolder.img_picture.setBackground(ContextCompat.getDrawable(mContext, R.mipmap.doc_default));
            } else {
                vBookViewHolder.ll_no_picture.setVisibility(View.VISIBLE);
                vBookViewHolder.tv_standard_status.setVisibility(View.VISIBLE);
            }
        } else {
            if (!imgUrl.contains(".rongrong")) {
                imgUrl = AppUtils.IMG2_URL + imgUrl;
            }
            vBookViewHolder.ll_no_picture.setVisibility(View.GONE);
            vBookViewHolder.tv_standard_status.setVisibility(View.GONE);

            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .error(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(false)
                    .transform(new CenterCrop(),new RoundedCorners(DensityUtils.dp2px(mContext,4f)))
                    .priority(Priority.NORMAL);

            Glide.with(mContext).load(imgUrl)
                    .apply(options)
                    .override(DensityUtils.dp2px(mContext, 115), DensityUtils.dp2px(mContext, 115))
                    .into(vBookViewHolder.img_picture);
        }



    }


    public class VBookViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_standard_status)
        TextView tv_standard_status;
        @BindView(R.id.tv_standard_price)
        TextView tv_standard_price;
        @BindView(R.id.tv_standard_no)
        TextView tv_standard_no;
        @BindView(R.id.tv_standard_name)
        TextView tv_standard_name;
        @BindView(R.id.img_shop_car)
        public ImageView img_shop_car;
        @BindView(R.id.tv_book_desc)
        TextView tv_book_desc;
        @BindView(R.id.img_picture)
        ImageView img_picture;//图片
        @BindView(R.id.ll_no_picture)
        LinearLayout ll_no_picture;
        @BindView(R.id.tv_standard_category)
        TextView tv_standard_category;//分类
        @BindView(R.id.tv_book_desc_sub)
        TextView tv_book_desc_sub;


        public VBookViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }


}
